package com.z.controller;


import com.z.pojo.Goods;
import com.z.service.GoodsService;
import com.z.service.impl.GoodsServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@WebServlet("/admin/manage/updateGoodsbygid.do")
public class UpdateGoodsServlet extends HttpServlet {
    private GoodsService goodsService = new GoodsServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        FileItemFactory fileItemFactory =new DiskFileItemFactory();
        ServletFileUpload sfu  = new ServletFileUpload(fileItemFactory);
        List<FileItem> fileItems=null;
        try {
            fileItems = sfu.parseRequest(req);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
//        for (FileItem fileItem : fileItems) {
//            System.out.println("fileItem.getFieldName() = " + fileItem.getFieldName());
//            System.out.println("fileItem.getContentType() = " + fileItem.getContentType());
//            System.out.println("fileItem.getName() = " + fileItem.getName());
//            System.out.println("fileItem.getSize() = " + fileItem.getSize());
//            System.out.println("fileItem.getString() = " + fileItem.getString("utf-8"));
//        }
        FileItem fileItem0 = fileItems.get(0);
        String gid = fileItem0.getString("utf-8");

        FileItem fileItem = fileItems.get(1);
        String gname = fileItem.getString("utf-8");

        FileItem fileItem1 = fileItems.get(2);
        String  tid  =fileItem1.getString("utf-8");

        FileItem fileItem2 = fileItems.get(3);
        String filename = fileItem2.getName();

        FileItem fileItem3 = fileItems.get(4);
        String price =fileItem3.getString("utf-8");

        FileItem fileItem4 = fileItems.get(5);
        String gdesc =fileItem4.getString("utf-8");

        Goods goods = new Goods();
        goods.setGid(Integer.valueOf(gid));
        goods.setPrice(Double.valueOf(price));
        goods.setGname(gname);
        goods.setGdesc(gdesc);

        if(filename==null||"".equals(filename)){

        }else {
            String savepath = getServletContext().getRealPath("/") + "upload\\";
            File savefile = new File(savepath);
            if(!savefile.isDirectory()){
                if(savefile.exists()){
                    savefile.delete();
                    savefile.mkdir();
                }else {
                    savefile.mkdir();
                }
            }
//      2.修改上传图片的名称。
//            1.确定上传文件名称没有路径/
//              1.windows linux  mac。
            if(filename.indexOf(File.separator)>-1){
                filename =filename.substring(filename.lastIndexOf(File.separator));
            }
//      修改名称
            String newName = UUID.randomUUID().toString()+"_"+new Date().getTime()+"_"+filename;
            File save = new File(savepath,newName);
            try {
                fileItem2.write(save);
            } catch (Exception e) {
                e.printStackTrace();
            }
            String usepath ="upload\\"+newName;
            goods.setG_img(usepath);

        }
        Integer flag = 0;

        try {
            flag = goodsService.updataGoods(goods);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        if(flag > 0){
            req.setAttribute("msg","修改成功");
            req.getRequestDispatcher("/admin/manage/adminfindgoodspage.do").forward(req,resp);
        }else {
            req.setAttribute("msg","修改失败");
            req.getRequestDispatcher("/admin/manage/adminfindgoodspage.do").forward(req,resp);
        }
    }
}
